﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace _超市收银系统
{
    //超市类
    internal class SuperMarket
    {
        //创建仓库对象
        Storehouse storeHouse = new Storehouse();

        /// <summary>
        /// 创建超市对象的时候，给超市的货架导入货物
        /// </summary>
        public SuperMarket()
        {
            storeHouse.GetGoods("Fish", 1000);
            storeHouse.GetGoods("Meat", 1000);
            storeHouse.GetGoods("Cabbage", 1000);
            storeHouse.GetGoods("Banana", 1000);
            storeHouse.GetGoods("Egg", 1000);
        }


        public void AskBuying()
        {
            Console.WriteLine("\n欢迎光临，萨哇嘀咔~请问您需要些什么？");
            Console.WriteLine("我们这里有 Fish、Meat、Cabbage、Banana、Egg");;
            string strType = Console.ReadLine();
            Console.WriteLine("您需要多少?");
            int count = 0;
            try
            {
                count = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("请重新输入上面货物名称!");
            }
            //去仓库取货
            WaresFather[] wares = storeHouse.SaleGoods(strType, count);
            //计算价钱
            double realMoney = GetMoney(wares);
            Console.WriteLine("您总共需要付 {0}元",realMoney);
            Console.WriteLine("请选择您的打着方式 1-不打折、2-打9折、3-打8.5折、4-买100送15、5-买50送5");
            string input = Console.ReadLine();
            //通过简单工厂设计模式根据用户的输入获得一个打折对象
            DiscountFather moneyFather = GetDiscount(input);
            double money = moneyFather.GetTotalMoney(realMoney);
            Console.WriteLine("打完折后您应该支付 {0}元", money);
            Console.WriteLine("谢谢您的惠顾 ，轰躲呢阿里嘎多~");
            Console.WriteLine("这是您的小票");
            foreach(var item in wares)
            {
                Console.WriteLine("货物名称:{0}\t" + "单价:{1}\t" + "数量:{2}\t" + "货物编号:{3}",item.Name, item.Price, item.Count, item.Id);
            }
        }

        /// <summary>
        /// 根据用户的选择打折方式返回一个打折对象
        /// </summary>
        /// <param name="input">用户的选择</param>
        /// <returns>返回父类对象，但是里面装的是子类对象</returns>
        public DiscountFather GetDiscount(string input)
        {
            DiscountFather discount = null;
            switch (input)
            {
                case "1": discount = new DiscountNormal(); //不打折
                    break;
                case "2": discount = new DiscountRate(0.9); //打9折
                    break;
                case "3": discount = new DiscountRate(0.85); //大8.5折
                    break;
                case "4": discount = new DiscountMoney(100,15); //买300送20
                    break;
                case "5":discount = new DiscountMoney(50, 5); //买500送50
                    break;
            }
            return discount;
        }

        /// <summary>
        /// 根据用户购买的货物计算总价钱
        /// </summary>
        /// <param name="wares">用户所买货物的种类及其数量</param>
        /// <returns>实际总价钱</returns>
        public double GetMoney(WaresFather[] wares)
        {
            double realMoney = 0;
            for(int i = 0; i < wares.Length; i++)
            {
                try
                {
                    realMoney += wares[i].Price;
                    //realMoney = wares[i] * wares.Length;
                }
                catch
                {
                    Console.WriteLine("请按照上面的商品名称重新输入!");
                }
            }
            return realMoney;
        }

        /// <summary>
        /// 展示货物
        /// </summary>
        public void ShowHouse()
        {
            //上面已创建了仓库对象(Storehouse) ,在超市类中获取货物展示的函数，可避免在创建超市对象之前展示货物时还没有把
            //货物导入仓库的情况。要先创建了超市对象后货物才能放入仓库中去。
            storeHouse.showGoods();
        }
    }
}
